/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scd.commons.result.Result;
import com.lap.scp.admin.ao.RoleAO;
import com.lap.scp.admin.domain.RoleDO;
import com.lap.scp.admin.query.RoleQueryDO;

/**
 * <pre>
 * 角色管理控制层
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Controller
@RequestMapping("role")
public class RoleController {

	@Resource
	private RoleAO roleAO;

	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("admin/roleEdit");
		andView.addObject("roleDO", new RoleDO());
		return andView;
	}

	@PostMapping(value = "add")
	public ModelAndView add(RoleDO roleDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();

		Result<RoleDO> resp = roleAO.insertRole(roleDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/role/list");
		} else {
			andView.setViewName("admin/roleEdit");
			andView.addObject("roleDO", roleDO);
		}
		return andView;
	}

	@GetMapping(value = "delete/{roleId}")
	public ModelAndView deleteById(@PathVariable int roleId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = roleAO.deleteById(roleId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/role/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "get/{roleId}")
	public ModelAndView queryById(@PathVariable int roleId) {
		ModelAndView andView = new ModelAndView();

		Resp<RoleDO> resp = roleAO.queryById(roleId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/roleDetail");
			andView.addObject("roleDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryList(Page page, RoleQueryDO queryDO) {
		ModelAndView andView = new ModelAndView();

		PageResult<RoleDO> pr = roleAO.queryPage(page, queryDO);
		if (pr != null) {
			andView.setViewName("admin/roleList");
			andView.addObject("pr", pr);
			andView.addObject("roleQueryDO", queryDO);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@PostMapping(value = "edit")
	public ModelAndView update(RoleDO roleDO, BindingResult error, @RequestParam Map<String, String> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = roleAO.updateRole(roleDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/role/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("admin/roleEdit");
			andView.addObject("roleDO", roleDO);
		}
		return andView;
	}

	@GetMapping(value = "edit/{roleId}")
	public ModelAndView update(@PathVariable int roleId) {
		ModelAndView andView = new ModelAndView();
		Resp<RoleDO> resp = roleAO.queryById(roleId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/roleEdit");
			andView.addObject("roleDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "bind/{roleId}")
	public ModelAndView bind(@PathVariable int roleId) {
		ModelAndView andView = new ModelAndView();
		Resp<RoleDO> resp = roleAO.queryForBind(roleId);
		if (resp.isSuccess()) {
			RoleDO roleDO = resp.getData();
			andView.setViewName("admin/roleBind");
			andView.addObject("roleDO", roleDO);

			andView.addObject("json", JSONArray.toJSONString(roleDO.getTreelist()));
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@PostMapping(value = "bind")
	public ModelAndView bind(@RequestParam Map<String, String> map, RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();
		int roleId = Integer.parseInt(String.valueOf(map.get("roleId")));
		Resp<Integer> resp = roleAO.roleResources(roleId, String.valueOf(map.get("checked")));
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/role/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("admin/roleBind");
		}
		return andView;
	}
}
